import cv2
import mediapipe as mp
import tools
import distinguish
from Pose import Dynamic_Data,Dynamic_Tools,Dynamic_Distinguish,Gesture_Record

cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)  # 摄像头
mp_Pose = mp.solutions.pose
pose = mp_Pose.Pose()  # 人体
mpHands = mp.solutions.hands
hands = mpHands.Hands()  # 手
mpDraw = mp.solutions.drawing_utils
mpDraw_styles = mp.solutions.drawing_styles
mp_holistic = mp.solutions.holistic

"""载入手势文件"""
"""这个是右挥拳"""
Dynamic_Data.Action1,Dynamic_Data.Action2,Dynamic_Data.Action3,Dynamic_Data.Action4,Dynamic_Data.Action5,Dynamic_Data.Action6,Dynamic_Data.Action7,Dynamic_Data.Action8=Gesture_Record.Connect2Data()

def run_Pose():
    while True:
        ret, img = cap.read()  # 一次循环截一张图
        if ret:
            imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)  # imgRGB是一张图片，cv拿到的图是bgr的，转成rgb
            result_pose = pose.process(imgRGB)  # 把这个图片给hands对象处理,result是侦测出来的手
            result_hand = hands.process(imgRGB)
            # print(result_pose.pose_landmarks)#提取手部21个点的坐标

            if result_pose.pose_landmarks:
                """如果采集到人体"""
                lm_1shot = Dynamic_Tools.Get_All_Landmark(result_pose.pose_landmarks)  # 不在摄像头内的部位会自己补全
                lm_1shot = Dynamic_Tools.transform_std(lm_1shot)  # 坐标变换和远近标准化
                """判断动作"""
                Dynamic_Distinguish.Dynamic(lm_1shot)

                """画图"""
                mpDraw.draw_landmarks(
                    img,
                    result_pose.pose_landmarks,
                    mp_holistic.POSE_CONNECTIONS,
                    landmark_drawing_spec=mpDraw_styles
                        .get_default_pose_landmarks_style())
                # if result_hand.multi_hand_landmarks:
                #     for handLms in result_hand.multi_hand_landmarks:  # handLms是一只手的21点
                #         mpDraw.draw_landmarks(img, handLms,mp_holistic.HAND_CONNECTIONS)  # img:画在img这个图片上，handLms：要画的点

            cv2.namedWindow('show')
            cv2.imshow('show', img)


        if cv2.waitKey(1) == ord('q'):  # 按q结束摄像头
            break

if __name__ == '__main__':
    run_Pose()